<template>
  <view class="product-filter">
    <!-- 筛选按钮 -->
    <view class="filter-trigger" @click="showFilter = true">
      <text class="filter-text">筛选</text>
      <text class="filter-icon">⚙</text>
      <view v-if="hasActiveFilters" class="filter-badge"></view>
    </view>

    <!-- 筛选弹窗 -->
    <uni-popup ref="filterPopup" type="bottom" :mask-click="false">
      <view class="filter-popup">
        <view class="filter-header">
          <text class="filter-title">商品筛选</text>
          <text class="filter-reset" @click="resetFilters">重置</text>
        </view>

        <scroll-view scroll-y class="filter-content">
          <!-- 商品分类 -->
          <view class="filter-section">
            <view class="section-title">商品分类</view>
            <view class="option-list">
              <view 
                v-for="category in categories" 
                :key="category.value"
                class="option-item"
                :class="{ active: filters.category === category.value }"
                @click="selectCategory(category.value)"
              >
                {{ category.label }}
              </view>
            </view>
          </view>

          <!-- 商品类型 -->
          <view class="filter-section">
            <view class="section-title">商品类型</view>
            <view class="option-list">
              <view 
                v-for="type in types" 
                :key="type.value"
                class="option-item"
                :class="{ active: filters.type === type.value }"
                @click="selectType(type.value)"
              >
                {{ type.label }}
              </view>
            </view>
          </view>

          <!-- 积分价格 -->
          <view class="filter-section">
            <view class="section-title">积分价格</view>
            <view class="price-range">
              <input 
                v-model="filters.minPrice" 
                type="number" 
                placeholder="最低价格"
                class="price-input"
              />
              <text class="price-separator">-</text>
              <input 
                v-model="filters.maxPrice" 
                type="number" 
                placeholder="最高价格"
                class="price-input"
              />
            </view>
          </view>

          <!-- 库存状态 -->
          <view class="filter-section">
            <view class="section-title">库存状态</view>
            <view class="option-list">
              <view 
                class="option-item"
                :class="{ active: filters.inStock === true }"
                @click="selectStock(true)"
              >
                有库存
              </view>
              <view 
                class="option-item"
                :class="{ active: filters.inStock === false }"
                @click="selectStock(false)"
              >
                包含缺货
              </view>
            </view>
          </view>

          <!-- 商品标签 -->
          <view class="filter-section">
            <view class="section-title">商品标签</view>
            <view class="tag-list">
              <view 
                class="tag-item"
                :class="{ active: filters.isHot }"
                @click="toggleHot"
              >
                热门商品
              </view>
              <view 
                class="tag-item"
                :class="{ active: filters.isNew }"
                @click="toggleNew"
              >
                新品商品
              </view>
            </view>
          </view>

          <!-- 排序方式 -->
          <view class="filter-section">
            <view class="section-title">排序方式</view>
            <view class="option-list">
              <view 
                v-for="sort in sortOptions" 
                :key="sort.value"
                class="option-item"
                :class="{ active: filters.sortBy === sort.value }"
                @click="selectSort(sort.value)"
              >
                {{ sort.label }}
              </view>
            </view>
          </view>
        </scroll-view>

        <view class="filter-footer">
          <button class="btn btn-secondary" @click="closeFilter">取消</button>
          <button class="btn btn-primary" @click="applyFilters">确定</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  name: 'ProductFilter',
  props: {
    value: {
      type: Object,
      default: () => ({})
    }
  },
  
  data() {
    return {
      showFilter: false,
      filters: {
        category: '',
        type: '',
        minPrice: '',
        maxPrice: '',
        inStock: null,
        isHot: false,
        isNew: false,
        sortBy: 'sort_order'
      },
      categories: [
        { label: '全部分类', value: '' },
        { label: '打印服务', value: 'print' },
        { label: '复印服务', value: 'copy' },
        { label: '装订服务', value: 'binding' },
        { label: '其他服务', value: 'other' }
      ],
      types: [
        { label: '全部类型', value: '' },
        { label: '黑白打印', value: 'black_white' },
        { label: '彩色打印', value: 'color' },
        { label: 'A4复印', value: 'a4_copy' },
        { label: 'A3复印', value: 'a3_copy' },
        { label: '螺旋装订', value: 'spiral_binding' },
        { label: '订书机装订', value: 'staple_binding' }
      ],
      sortOptions: [
        { label: '默认排序', value: 'sort_order' },
        { label: '价格从低到高', value: 'points_price_asc' },
        { label: '价格从高到低', value: 'points_price_desc' },
        { label: '最新发布', value: 'created_at' },
        { label: '库存从多到少', value: 'stock_quantity' }
      ]
    }
  },
  
  computed: {
    hasActiveFilters() {
      return this.filters.category || 
             this.filters.type || 
             this.filters.minPrice || 
             this.filters.maxPrice || 
             this.filters.inStock !== null ||
             this.filters.isHot || 
             this.filters.isNew ||
             this.filters.sortBy !== 'sort_order'
    }
  },
  
  watch: {
    value: {
      handler(newVal) {
        this.filters = { ...this.filters, ...newVal }
      },
      immediate: true,
      deep: true
    },
    
    showFilter(show) {
      if (show) {
        this.$refs.filterPopup.open()
      } else {
        this.$refs.filterPopup.close()
      }
    }
  },
  
  methods: {
    selectCategory(category) {
      this.filters.category = this.filters.category === category ? '' : category
    },
    
    selectType(type) {
      this.filters.type = this.filters.type === type ? '' : type
    },
    
    selectStock(inStock) {
      this.filters.inStock = this.filters.inStock === inStock ? null : inStock
    },
    
    toggleHot() {
      this.filters.isHot = !this.filters.isHot
    },
    
    toggleNew() {
      this.filters.isNew = !this.filters.isNew
    },
    
    selectSort(sortBy) {
      this.filters.sortBy = sortBy
    },
    
    resetFilters() {
      this.filters = {
        category: '',
        type: '',
        minPrice: '',
        maxPrice: '',
        inStock: null,
        isHot: false,
        isNew: false,
        sortBy: 'sort_order'
      }
    },
    
    applyFilters() {
      // 处理排序参数
      let sortBy = 'sort_order'
      let sortOrder = 'DESC'
      
      if (this.filters.sortBy === 'points_price_asc') {
        sortBy = 'points_price'
        sortOrder = 'ASC'
      } else if (this.filters.sortBy === 'points_price_desc') {
        sortBy = 'points_price'
        sortOrder = 'DESC'
      } else if (this.filters.sortBy === 'created_at') {
        sortBy = 'created_at'
        sortOrder = 'DESC'
      } else if (this.filters.sortBy === 'stock_quantity') {
        sortBy = 'stock_quantity'
        sortOrder = 'DESC'
      }
      
      const filterParams = {
        category: this.filters.category || undefined,
        type: this.filters.type || undefined,
        minPrice: this.filters.minPrice || undefined,
        maxPrice: this.filters.maxPrice || undefined,
        inStock: this.filters.inStock,
        isHot: this.filters.isHot || undefined,
        isNew: this.filters.isNew || undefined,
        sortBy,
        sortOrder
      }
      
      this.$emit('filter', filterParams)
      this.closeFilter()
    },
    
    closeFilter() {
      this.showFilter = false
    }
  }
}
</script>

<style lang="scss" scoped>
.product-filter {
  position: relative;
}

.filter-trigger {
  display: flex;
  align-items: center;
  padding: 12rpx 20rpx;
  background-color: #f5f5f5;
  border-radius: 20rpx;
  position: relative;
}

.filter-text {
  font-size: 26rpx;
  color: #666;
  margin-right: 8rpx;
}

.filter-icon {
  font-size: 24rpx;
  color: #666;
}

.filter-badge {
  position: absolute;
  top: 4rpx;
  right: 4rpx;
  width: 12rpx;
  height: 12rpx;
  background-color: #ff4757;
  border-radius: 50%;
}

.filter-popup {
  background-color: #fff;
  border-radius: 20rpx 20rpx 0 0;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.filter-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.filter-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.filter-reset {
  font-size: 28rpx;
  color: #007aff;
}

.filter-content {
  flex: 1;
  padding: 0 30rpx;
}

.filter-section {
  margin-bottom: 40rpx;
}

.section-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
}

.option-list {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
}

.option-item {
  padding: 12rpx 24rpx;
  background-color: #f5f5f5;
  border-radius: 20rpx;
  font-size: 26rpx;
  color: #666;
  
  &.active {
    background-color: #007aff;
    color: #fff;
  }
}

.price-range {
  display: flex;
  align-items: center;
  gap: 16rpx;
}

.price-input {
  flex: 1;
  padding: 16rpx;
  border: 1rpx solid #e0e0e0;
  border-radius: 8rpx;
  font-size: 26rpx;
}

.price-separator {
  font-size: 26rpx;
  color: #666;
}

.tag-list {
  display: flex;
  gap: 16rpx;
}

.tag-item {
  padding: 12rpx 24rpx;
  background-color: #f5f5f5;
  border-radius: 20rpx;
  font-size: 26rpx;
  color: #666;
  
  &.active {
    background-color: #ff4757;
    color: #fff;
  }
}

.filter-footer {
  display: flex;
  gap: 20rpx;
  padding: 30rpx;
  border-top: 1rpx solid #f0f0f0;
}

.btn {
  flex: 1;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  border-radius: 40rpx;
  font-size: 28rpx;
  border: none;
}

.btn-secondary {
  background-color: #f0f0f0;
  color: #666;
}

.btn-primary {
  background-color: #007aff;
  color: #fff;
}
</style>